//
// PieTimeout.cs
//
// Copyright (C) 2008 Lukas Lipka <lukaslipka@gmail.com>
//

using System;
using System.Collections.Generic;

using Gtk;
using Dashboard.Mint.Theatrics;

namespace Dashboard.Mint.Widgets {

	public class PieTimeout : Gtk.Alignment {

		private Stage<PieTimeout> stage = null;
		private Actor<PieTimeout> actor = null;

		private double radius = 0.0f;

		public event EventHandler Timeout;

		public PieTimeout (uint timeout) : base (0.0f, 0.0f, 1.0f, 1.0f)
		{
			base.AppPaintable = true;
			base.BorderWidth = 5;

			stage = new Stage<PieTimeout> ();
			stage.Iteration += delegate { QueueDraw (); };

			actor = new Actor<PieTimeout> (this, timeout);
			stage.Add (actor);
		}

		public void Start ()
		{
			stage.Start ();
		}

		public void Stop ()
		{
			stage.Stop ();
		}

		protected override void OnSizeRequested (ref Requisition requisition)
		{
			requisition.Width = 32;
			requisition.Height = 32;
		}

		protected override void OnSizeAllocated (Gdk.Rectangle rect)
		{
			radius = (Math.Min (rect.Width, rect.Height) / 2) - BorderWidth;
			base.OnSizeAllocated (rect);
		}

		protected override bool OnExposeEvent (Gdk.EventExpose evnt)
		{
			if (!IsDrawable)
				return true;

			Cairo.Context gr = Gdk.CairoHelper.Create (evnt.Window);

			CairoFu.Pie (gr, this, Allocation.X + (Allocation.Width / 2), Allocation.Y + (Allocation.Height / 2), radius, 1.0 - actor.Percent, 0.5);

			CairoFu.DisposeContext (gr);
			
			return base.OnExposeEvent (evnt);
		}

		private void OnTimeout (object o, EventArgs args)
		{
			Hide ();
		}

		public bool HideOnTimeout {
			set {
				if (value == true)
					stage.Stopped += OnTimeout;
				else
					stage.Stopped -= OnTimeout;
			}
		}
	}
}
